/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.apt.citytag.process;

import com.apt.tag.Connect_DB;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Bach
 */
public class EditCityName {
    public static boolean setEditCity(String city_Name, String city_id) {
        String up = "UPDATE [Matrimony].[dbo].[Cities]\n"
                + "   SET [City_Name] = ? \n"
                + " WHERE [City_Id]=?";
        boolean re = false;
        Connection con = null;
        PreparedStatement ps = null;
        try {
            con = Connect_DB.getConnection();
            if (checkusername(city_Name, con)) {
                ps = con.prepareStatement(up);
                ps.setString(1, city_Name);
                ps.setInt(2, Integer.parseInt(city_id));
                int i = ps.executeUpdate();
                if (i > 0) {
                    re = true;
                }
            } else {
                System.out.println("UPDATE [Matrimony].[dbo].[Cities] Fail");
            }
        } catch (SQLException e) {
            e.printStackTrace();
            try {
                con.rollback();
            } catch (SQLException ex) {
                Logger.getLogger(EditCityName.class.getName()).log(Level.SEVERE, null, ex);
            }
        } finally {
            Connect_DB.close(null, null, ps, con);
        }
        return re;
    }

    public static boolean checkusername(String name, Connection cnn) {
        boolean re = true;
        PreparedStatement ps = null;
        String qurey = "SELECT  [City_Id]\n"
                + "      ,[Country_Id]\n"
                + "      ,[City_Name]\n"
                + "      ,[City_Status]\n"
                + "  FROM [Matrimony].[dbo].[Cities] "
                + "Where [City_Name]=? ";
        ResultSet rs = null;
        try {
            ps = cnn.prepareStatement(qurey);
            ps.setString(1, name);
            rs = ps.executeQuery();
            if (rs.next()) {
                re = false;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return re;
    }
}
